/*
 * @Description:
 * @Author: owen
 * @Date: 2021-03-31 19:54:20
 * @LastEditTime: 2023-03-29 11:05:25
 */
import { optObject } from '../config'
import { isObject } from '@/utils/tools'
import { has, curry, equals, isEmpty } from 'ramda'
import { BasicDataModule } from '@/store/modules/basicData'
import { UserDataModule } from '@/store/modules/user'
function isRableSlotScope(arg: any): arg is F.RableSlotScope {
	return isObject(arg)
}
type Key = string | number | F.RableSlotScope

export const configFilter = (prop: string, key: Key) => {
	if (isRableSlotScope(key)) {
		key = key.text
	}
	if (has(prop, optObject)) {
		return optObject[prop][key as string]
	}
	return key
}
type propKey = 'categoryList' | 'tagsList'
/** 基础数据过滤 */
export const basicDataFilter = function (prop: propKey, key: Key) {
	if (isRableSlotScope(key)) {
		key = key.text
	}
	if (isEmpty(BasicDataModule[prop])) {
		const Action = ('get' + prop.slice(0, 1).toUpperCase() + prop.slice(1)) as 'getCategoryList'
		BasicDataModule[Action]()
		return key
	}
	const data = BasicDataModule[prop]?.find((item) => equals(item.value, key))
	if (data) {
		return data.label
	}
	return key
}
type uerPropKey = 'roleList' | 'userList' | 'homeUserList'
/** 基础数据过滤 */
export const userDataFilter = function (prop: uerPropKey, key: Key) {
	if (isRableSlotScope(key)) {
		key = key.text
	}
	if (isEmpty(UserDataModule[prop])) {
		const Action = ('get' + prop.slice(0, 1).toUpperCase() + prop.slice(1)) as 'getRoleList'
		UserDataModule[Action]()
		return key
	}
	const data = UserDataModule[prop].find((item) => equals(item.value, key))
	if (data) {
		return data.label
	}
	return key
}
export const configCurryFilter = curry(configFilter)
export const basicDataCurryFilter = curry(basicDataFilter)
export const userDataCurryFilter = curry(userDataFilter)
